<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('上市地')" prop="listingPlaceArr">
            <dict-select
              v-model="form.listingPlaceArr"
              dictionary="listingPlan-ListingPlace"
              multiple
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基准日')" prop="baseDay">
            <el-date-picker
              v-model="form.baseDay"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('基准日')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('报材料时间')" prop="reportingTime">
            <el-date-picker
              v-model="form.reportingTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('报材料时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('计划上市时间')" prop="plannedTimeToMarket">
            <el-date-picker
              v-model="form.plannedTimeToMarket"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('计划上市时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('券商')" prop="brokerage">
            <el-select v-model="form.brokerage" :placeholder="$t('券商')">
              <el-option
                v-for="mediation in brokerageMediationList"
                :key="mediation.id"
                :label="$t(mediation.mediationName)"
                :value="mediation.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('会计师事务所')" prop="accountingFirm">
            <el-select v-model="form.accountingFirm" :placeholder="$t('会计师事务所')">
              <el-option
                v-for="mediation in accountingFirmMediationList"
                :key="mediation.id"
                :label="$t(mediation.mediationName)"
                :value="mediation.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('律师事务所')" prop="lawOffice">
            <el-select v-model="form.lawOffice" :placeholder="$t('律师事务所')">
              <el-option
                v-for="mediation in lawOfficeMediationList"
                :key="mediation.id"
                :label="$t(mediation.mediationName)"
                :value="mediation.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('定价')" prop="fixedPrice">
            <Money v-model="form.fixedPrice" :placeholder="$t('定价')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('上市进程')" prop="remark">
            <dict-select
              v-model="form.remark"
              dictionary="listingProcess"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="listingPlan" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <save-btn ref="formBtn" :bus-data="form" has-permi="yueyin:listingPlan" />
    </div>
  </div>
</template>

<script>
import { saveSysYueyinDealListingPlan, getSysYueyinDealListingPlan } from '@/api/sass/yueyin/deal/listingPlan/sysYueyinDealListingPlan'
import { listMediationBase } from '@/api/deal/mediation/mediationBase'

export default {
  data() {
    return {
      // 表单参数
      form: {
        listingPlaceArr: []
      },
      // 表单校验
      rules: {
        listingPlaceArr: [
          { type: 'array', required: true, message: '必填属性', trigger: 'change' }
        ],
        baseDay: [
          { required: true, message: '必填属性', trigger: 'blur' }
        ],
        reportingTime: [
          { required: true, message: '必填属性', trigger: 'blur' }
        ],
        plannedTimeToMarket: [
          { required: true, message: '必填属性', trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      brokerageMediationList: [],
      lawOfficeMediationList: [],
      accountingFirmMediationList: []

    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getSysYueyinDealListingPlan(data.id).then(response => {
          this.form = response.data
          // 上市地是多选的
          if (this.form.listingPlace !== undefined && this.form.listingPlace !== null && this.form.listingPlace !== '') {
            this.$set(this.form, 'listingPlaceArr', this.form.listingPlace.split(','))
          } else {
            this.$set(this.form, 'listingPlaceArr', [])
          }
          this.loading = false
        })
      } else {
        this.reset()
        console.log(this.form)
        this.loading = false
      }
      // 查询券商、会计师事务所、律师事务所 中介机构列表
      listMediationBase({ mediationType: 'QS' }).then(result => {
        this.brokerageMediationList = result.rows
      })
      listMediationBase({ mediationType: 'KJS' }).then(result => {
        this.lawOfficeMediationList = result.rows
      })
      listMediationBase({ mediationType: 'LS' }).then(result => {
        this.accountingFirmMediationList = result.rows
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        dealId: this.$route.query.id,

        listingPlace: undefined,

        baseDay: undefined,

        reportingTime: undefined,

        plannedTimeToMarket: undefined,

        brokerage: undefined,

        accountingFirm: undefined,

        lawOffice: undefined,

        fixedPrice: undefined,

        actId: undefined,

        actStatus: undefined,

        actTitle: undefined,

        remark: undefined,

        createBy: undefined,

        updateBy: undefined,

        createTime: undefined,

        updateTime: undefined,

        delFlag: undefined,
        listingPlaceArr: []

      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      this.$refs['form'].validate(valid => {
        // 验证数据
        if (valid) {
          // 弹出遮罩层
          this.loading = true
          // 上市地是多选的
          this.form.listingPlace = this.form.listingPlaceArr.join(',')
          // 保存数据
          saveSysYueyinDealListingPlan(this.form).then(response => {
            if (response.code === 200) {
              // 点击的按钮是保存并启动流程
              if (type === 'process') {
                // 启动流程
                this.startProcess(response.data)
                // 点击的仅保存
              } else {
                this.msgSuccess(this.$t('保存成功'))
                // 关闭form页面
                this.closeForm()
              }
            } else {
              this.msgError(response.msg)
              this.loading = false
            }
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 启动流程
    startProcess: function(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.$t('请根据需求设置待办标题')
      // 业务表名
      processData.dataTable = this.$t('业务表名')
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/url'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.$t('菜单标识')
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }

  }

}
</script>
